home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 February: Technology Seed / Mac Tech Seed Feb '97.toast / OpenDoc 1.2b2c1 / Implementation / Utilities / PrinterQD.cpp < prev    next >
Encoding:
Text File  |  1997-02-13  |  10.1 KB  |  436 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        PrinterQD.cpp
  3.  
  4.     Contains:    QuickDraw support for the OpenDoc printing utilities.
  5.  
  6.     Owned by:    Jens Alfke
  7.  
  8.     Copyright:    © 1995-96 by Apple Computer, Inc., all rights reserved.
  9.  
  10.     Change History (most recent first):
  11.  
  12.          <3>     12/6/96    TW        1605706 Saved and restored current grafport
  13.                                     to work around problem with LW 8.4.1-2
  14.          <2>      2/2/96    JA        Filled in header comments
  15.          <1>      2/2/96    JA        first checked in
  16.  
  17.     To Do:
  18. */
  19.  
  20.  
  21. // -- OpenDoc --
  22.  
  23. #ifndef _ALTPOINT_
  24. #include <AltPoint.h>
  25. #endif
  26.  
  27. #ifndef SOM_ODCanvas_xh
  28. #include <Canvas.xh>
  29. #endif
  30.  
  31. #ifndef SOM_ODClipboard_xh
  32. #include <Clipbd.xh>
  33. #endif
  34.  
  35. #ifndef SOM_Module_OpenDoc_Commands_defined
  36. #include <CmdDefs.xh>
  37. #endif
  38.  
  39. #ifndef SOM_ODDispatcher_xh
  40. #include <Disptch.xh>
  41. #endif
  42.  
  43. #ifndef SOM_ODFacet_xh
  44. #include <Facet.xh>
  45. #endif
  46.  
  47. #ifndef SOM_ODFrame_xh
  48. #include <Frame.xh>
  49. #endif
  50.  
  51. #ifndef SOM_ODMenuBar_xh
  52. #include <MenuBar.xh>
  53. #endif
  54.  
  55. #ifndef SOM_ODSession_xh
  56. #include <ODSessn.xh>
  57. #endif
  58.  
  59. #ifndef SOM_ODStorageSystem_xh
  60. #include <ODStor.xh>
  61. #endif
  62.  
  63. #ifndef SOM_ODPart_xh
  64. #include <Part.xh>
  65. #endif
  66.  
  67. #ifndef SOM_ODPlatformTypeList_xh
  68. #include <PfTypLs.xh>
  69. #endif
  70.  
  71. #ifndef SOM_ODShape_xh
  72. #include <Shape.xh>
  73. #endif
  74.  
  75. #ifndef SOM_Module_OpenDoc_StdProps_defined
  76. #include <StdProps.xh>
  77. #endif
  78.  
  79. #ifndef SOM_Module_OpenDoc_StdTypes_defined
  80. #include <StdTypes.xh>
  81. #endif
  82.  
  83. #ifndef SOM_ODTransform_xh
  84. #include <Trnsform.xh>
  85. #endif
  86.  
  87. #ifndef SOM_ODWindowState_xh
  88. #include <WinStat.xh>
  89. #endif
  90.  
  91. // -- OpenDoc Utilities --
  92.  
  93. #ifndef _EXCEPT_
  94. #include <Except.h>
  95. #endif
  96.  
  97. #ifndef _ITEXT_
  98. #include <IText.h>
  99. #endif
  100.  
  101. #ifndef _DLOGUTIL_
  102. #include <DlogUtil.h>
  103. #endif
  104.  
  105. #ifndef _ODDEBUG_
  106. #include <ODDebug.h>
  107. #endif
  108.  
  109. #ifndef _ODNEW_
  110. #include <ODNew.h>
  111. #endif
  112.  
  113. #ifndef _ODUTILS_
  114. #include <ODUtils.h>
  115. #endif
  116.  
  117. #ifndef _STORUTIL_
  118. #include <StorUtil.h>
  119. #endif
  120.  
  121. #ifndef _TEMPOBJ_
  122. #include <TempObj.h>
  123. #endif
  124.  
  125. #ifndef _USERSRCM_
  126. #include <UseRsrcM.h>
  127. #endif
  128.  
  129. // -- MacToolbox --
  130.  
  131. #ifndef __TEXTUTILS__
  132. #include <TextUtils.h>
  133. #endif
  134.  
  135. // -- Printer --
  136. #define _PRINTER_PRIVATE_
  137. #ifndef _PRINTER_
  138. #include "Printer.h"
  139. #endif
  140.  
  141. #pragma segment Printer
  142.  
  143.  
  144. //---------------------------------------------------------------------------------
  145. // THROW_IF_PRINT_ERROR  [static]
  146. //---------------------------------------------------------------------------------
  147.  
  148. static void
  149. THROW_IF_PRINT_ERROR( )
  150. {
  151.     OSErr err = PrError();
  152.     if( err ) THROW(err);
  153. }
  154.  
  155.  
  156. //=================================================================================
  157. // CQDPrinter implementation
  158. //=================================================================================
  159.  
  160. //---------------------------------------------------------------------------------
  161. // CQDPrinter::CQDPrinter
  162. //---------------------------------------------------------------------------------
  163.  
  164. CQDPrinter::CQDPrinter()
  165. {
  166.     fJob = kODNULL;
  167.     fPort = kODNULL;
  168.     fIOBuffer = kODNULL;
  169.     fPrintingOpen = 0;
  170.     fPageOpen = kODFalse;
  171. }
  172.  
  173. //---------------------------------------------------------------------------------
  174. // CQDPrinter::~CQDPrinter
  175. //---------------------------------------------------------------------------------
  176.  
  177. CQDPrinter::~CQDPrinter()
  178. {
  179.     ODDisposeHandle((ODHandle)fJob);
  180.     ODDisposePtr(fPort);                    // Should have been disposed earlier, but...
  181.     ODDisposePtr(fIOBuffer);
  182. }
  183.  
  184. //---------------------------------------------------------------------------------
  185. // CQDPrinter::OpenPrinting
  186. //---------------------------------------------------------------------------------
  187.  
  188. void CQDPrinter::OpenPrinting()
  189. {
  190.     PrOpen();
  191.     THROW_IF_PRINT_ERROR();
  192.     inherited::OpenPrinting();
  193. }
  194.  
  195. //---------------------------------------------------------------------------------
  196. // CQDPrinter::ClosePrinting
  197. //---------------------------------------------------------------------------------
  198.  
  199. void CQDPrinter::ClosePrinting()
  200. {
  201.     inherited::ClosePrinting();
  202.     PrClose();
  203. }
  204.  
  205. //---------------------------------------------------------------------------------
  206. // CQDPrinter::ReadJobFromHandle
  207. //---------------------------------------------------------------------------------
  208.  
  209. ODPlatformPrintJob CQDPrinter::ReadJobFromHandle(ODValueType valueType, ODHandle h)
  210. {
  211.     ASSERT(valueType==kODTypeQuickDrawPageSetup,kODErrInvalidGraphicsSystem);
  212.     WASSERT(fJob==kODNULL);
  213.     
  214.     fJob = (THPrint) h;
  215.     return (ODPlatformPrintJob)fJob;
  216. }
  217.  
  218. //---------------------------------------------------------------------------------
  219. // CQDPrinter::CreateNewJob
  220. //---------------------------------------------------------------------------------
  221.  
  222. ODPlatformPrintJob CQDPrinter::CreateNewJob()
  223. {
  224.     fJob = (THPrint) ODNewHandle(sizeof(TPrint));
  225.     PrintDefault(fJob);
  226.     if( PrError() ) {
  227.         ODDisposeHandle((ODHandle)fJob);
  228.         fJob = kODNULL;
  229.         THROW(PrError());
  230.     }
  231.     return (ODPlatformPrintJob)fJob;
  232. }
  233.  
  234. //---------------------------------------------------------------------------------
  235. // CQDPrinter::CopyJobToHandle
  236. //---------------------------------------------------------------------------------
  237.  
  238. ODHandle CQDPrinter::CopyJobToHandle( )
  239. {
  240.     ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  241.     return ODCopyHandle((ODHandle)fJob);
  242. }
  243.  
  244. //---------------------------------------------------------------------------------
  245. // CQDPrinter::CreatePrintingPlatformCanvas
  246. //---------------------------------------------------------------------------------
  247.  
  248. ODPlatformCanvas CQDPrinter::CreatePrintingPlatformCanvas( Environment*, ODGraphicsSystem g,
  249.                                                             ODFrame*, ODShape* )
  250. {
  251.     ASSERT(g==kODQuickDraw,kODErrInvalidGraphicsSystem);
  252.     WASSERT(!fPort);
  253.     WASSERT(!fIOBuffer);
  254.     
  255.     fPort = (TPrPort*) ODNewPtr(sizeof(TPrPort));
  256.     fIOBuffer = (Ptr) ODNewPtr(522);
  257.     
  258.     GrafPtr curPort;
  259.     GetPort(&curPort);
  260.     
  261.     PrOpenDoc(fJob, fPort, fIOBuffer);
  262.     
  263.     SetPort(curPort);                        // PrOpenDoc changes current port
  264.     
  265.     if( PrError() ) {
  266.         ODDisposePtr(fPort);
  267.         ODDisposePtr(fIOBuffer);
  268.         fPort = kODNULL;
  269.         fIOBuffer = kODNULL;
  270.         THROW(PrError());
  271.     }
  272.     return (ODPlatformCanvas) fPort;
  273. }
  274.  
  275. //---------------------------------------------------------------------------------
  276. // CQDPrinter::CleanupPrintingEnv
  277. //---------------------------------------------------------------------------------
  278.  
  279. void CQDPrinter::CleanupPrintingEnv(Environment* ev, ODFrame* initiator)
  280. {
  281.     // This routine should not throw exeptions and must call the inherited method.
  282.     
  283.     if( fPort ) {
  284.         TRY{
  285.             this->ClosePage(ev);                // In case it was left open
  286.         }CATCH_ALL{
  287.         }ENDTRY
  288.         
  289.         PrCloseDoc(fPort);
  290.         
  291.         // Spool document if necessary for this printer:
  292.         if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  293.             TPrStatus theStatus;
  294.             PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  295.         }
  296.         
  297.         ODDisposePtr(fIOBuffer);
  298.         ODDisposePtr(fPort);
  299.         fPort = kODNULL;
  300.         fIOBuffer = kODNULL;
  301.     }
  302.     
  303.     inherited::CleanupPrintingEnv(ev,initiator);
  304. }
  305.  
  306. //---------------------------------------------------------------------------------
  307. // CQDPrinter::GetPageRect
  308. //---------------------------------------------------------------------------------
  309.  
  310. ODRect CQDPrinter::GetPageRect( Environment *ev )
  311. {
  312.     this->GetPlatformPrintJob(ev);        // Force internalization of fJob
  313.     
  314.     return (ODRect) (**fJob).prInfo.rPage;
  315. }
  316.  
  317. //---------------------------------------------------------------------------------
  318. // CQDPrinter::OpenPage
  319. //---------------------------------------------------------------------------------
  320.  
  321. void CQDPrinter::OpenPage( Environment*, ODUShort page )
  322. {
  323.     if( !fPageOpen ) {
  324.         PrOpenPage(fPort, kODNULL);
  325.         THROW_IF_PRINT_ERROR();
  326.         fPageOpen = kODTrue;
  327.     }
  328. }
  329.  
  330. //---------------------------------------------------------------------------------
  331. // CQDPrinter::ClosePage
  332. //---------------------------------------------------------------------------------
  333.  
  334. void CQDPrinter::ClosePage( Environment* )
  335. {
  336.     if( fPageOpen ) {
  337.         PrClosePage(fPort);
  338.         fPageOpen = kODFalse;
  339.         THROW_IF_PRINT_ERROR();
  340.     }
  341. }
  342.  
  343. //---------------------------------------------------------------------------------
  344. // CQDPrinter::RunPageSetupDialog
  345. //---------------------------------------------------------------------------------
  346.  
  347. ODBoolean CQDPrinter::RunPageSetupDialog( Environment* )
  348. {
  349.     PrValidate(fJob);
  350.     THROW_IF_PRINT_ERROR();
  351.     
  352.     GrafPtr oldPort;
  353.     GetPort(&oldPort);
  354.     
  355.     ODBoolean success = PrStlDialog(fJob);
  356.     
  357.     SetPort(oldPort);                // PrStlDialog might change the current port
  358.     
  359.     THROW_IF_PRINT_ERROR();
  360.     return success;
  361. }
  362.  
  363. //---------------------------------------------------------------------------------
  364. // CQDPrinter::RunPrintDialog
  365. //---------------------------------------------------------------------------------
  366.  
  367. ODBoolean CQDPrinter::RunPrintDialog( Environment* )
  368. {
  369.     ODBoolean success;
  370.     TRY{
  371.         ASSERT(fJob!=kODNULL,kODErrAssertionFailed);
  372.         
  373.         PrValidate(fJob);
  374.         THROW_IF_PRINT_ERROR();
  375.  
  376.         GrafPtr oldPort;
  377.         GetPort(&oldPort);
  378.     
  379.         success = PrJobDialog(fJob);
  380.  
  381.         SetPort(oldPort);            // PrJobDialog might change the current port
  382.  
  383.         THROW_IF_PRINT_ERROR();
  384.         
  385.     }CATCH_ALL{
  386.         WARN("Error %d in page setup dialog",ErrorCode());
  387.         success = kODFalse;
  388.         // ignore exception
  389.     }ENDTRY
  390.     return success;
  391. }
  392.  
  393. //---------------------------------------------------------------------------------
  394. // CQDPrinter::PrintDocument
  395. //---------------------------------------------------------------------------------
  396.  
  397. void CQDPrinter::DoPrint(Environment* ev, ODShape* area)
  398. {
  399.     // Here's what you've been waiting for: the print loop.
  400.     // See Tech Note PR10 "A Printing Loop That Cares" for more details.
  401.     
  402.     // Extract page range from print job:
  403.     short firstPage = (**fJob).prJob.iFstPage;
  404.     short lastPage = (**fJob).prJob.iLstPage;
  405.     ODULong pageCount = this->CountPages(ev, area);
  406.     if ( lastPage > pageCount ) 
  407.         lastPage = pageCount;
  408.     (**fJob).prJob.iFstPage = 1;
  409.     (**fJob).prJob.iLstPage = iPrPgMax;
  410.  
  411.     // Print the whole ting as many times as the user wants.
  412.     short totalPages = 1;
  413.     for( short copy = (**fJob).prJob.iCopies; copy>0 ; copy-- )
  414.     {
  415.         // Print the pages, one at a time.
  416.         for (short page = firstPage; page <= lastPage; page++, totalPages++)
  417.         {
  418.             if (totalPages % iPFMaxPgs == 0)
  419.             {
  420.                 // Every 128 pages we must close, spool, and re-open printing:
  421.                 PrCloseDoc(fPort);
  422.                 if( (**fJob).prJob.bJDocLoop == bSpoolLoop && PrError() == noErr ) {
  423.                     TPrStatus theStatus;
  424.                     PrPicFile(fJob, fPort, fIOBuffer, kODNULL, &theStatus);
  425.                 }
  426.                 PrOpenDoc(fJob, fPort, (Ptr)fIOBuffer);
  427.                 THROW_IF_PRINT_ERROR();
  428.             }
  429.             
  430.             this->PrintPage(ev, page, area);                // Shazam!
  431.         
  432.             THROW_IF_PRINT_ERROR();
  433.         }
  434.     }
  435. }
  436.